﻿using AutoMapper;
using MediatR;
using S.Parturition.Infrastructure.Interface;
using S.Parturition.Read.API.Application.Command;
using S.Parturition.Read.API.Dto;

namespace S.Parturition.Read.API.Application.CommandHandlers
{
    /// <summary>
    /// 根据id反填种母分娩具体实现
    /// </summary>
    public class GetParturitionByIdHandler : IRequestHandler<GetParturitionByIdCommand, ParturitionListDto>
    {

        private readonly IParturitionRepository repository;

        private readonly IMapper mapper;

        public GetParturitionByIdHandler(IParturitionRepository repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<ParturitionListDto> Handle(GetParturitionByIdCommand request, CancellationToken cancellationToken)
        {
            var list = repository.GetAllAsync().Result.Where(x => x.ParturitionId == request.ParturitionId).FirstOrDefault();

            return mapper.Map<ParturitionListDto>(list);
        }
    }
}
